package com.lottery.ruok.lottrey;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.os.StrictMode;
import android.provider.Settings;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;

import com.applozic.mobicomkit.uiwidgets.conversation.messageUtils.BubbleUtils;
import com.applozic.mobicomkit.uiwidgets.conversation.messageUtils.ChatRecordUtils;
import com.applozic.mobicomkit.uiwidgets.conversation.messageUtils.SearchRecordUtils;
import com.lottery.ruok.lottrey.chat.RongCloudChatManager;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.dialog.FingerPrintDialog;
import com.lottery.ruok.lottrey.userInfo.UserInfo;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;

import count_down.CountDownManager;
import property.SharePreferenceProperties;
import utils.LogUtil;
import utils.Utils;


/**
 * Created by ASUS on 2017/7/27.
 */

public class MyApplication extends MultiDexApplication {


    public static String UUID;

    public int count = 0;
    public static boolean isStart = true;

    /** 全局Context，原理是因为Application类是应用最先运行的，所以在我们的代码调用时，该值已经被赋值过了 */
    private static MyApplication mInstance;
    /** 主线程ID */
    private static int mMainThreadId = -1;
    /** 主线程ID */
    private static Thread mMainThread;
    /** 主线程Handler */
    private static Handler mMainThreadHandler;
    /** 主线程Looper */
    private static Looper mMainLooper;
    private static boolean mResume;
    private static boolean mPendingKill;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);


        // 安装tinker
        Beta.installTinker();
    }

    @Override
    public void onCreate() {
        mMainThreadId = Process.myTid();
        mMainThread = Thread.currentThread();
        mMainThreadHandler = new Handler();
        mMainLooper = getMainLooper();
        mInstance = this;
        Utils.init(this);
        Bugly.init(this, BuildConfig.TINKER_ID, true);
        CountDownManager.asInstance().startCountDown();

        SharePreferenceProperties.setup(this);

        super.onCreate();

        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());

        UUID = Settings.System.getString(getContentResolver(), Settings.System.ANDROID_ID);


        if(BuildConfig.DEBUG) {
            // 初始化Looger工具
            LogUtil.init(BuildConfig.DEBUG);
        }

//        Fabric.with(this, new Crashlytics());

        Common.getIns().init(getApplicationContext());

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityStopped(Activity activity) {
                count--;
            }

            @Override
            public void onActivityStarted(Activity activity) {
                if (count == 0) {
                    if(!isStart && UserInfo.getFingerLock()){
//                        fingerPrintDialog = new FingerPrintDialog();
//                        if(activity instanceof BaseActivity) {
//                            fingerPrintDialog.show(((BaseActivity) activity).getSupportFragmentManager(), null);
                            startActivity(new Intent(activity, FingerPrintDialog.class));
//                        }
                    }
                }
                count++;
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityResumed(Activity activity) {
                LogUtil.d("CurrentActivity:"+activity.getLocalClassName());
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            }
        });

        BubbleUtils.initData(this);
        ChatRecordUtils.init();
        SearchRecordUtils.init();
//        RongIM.init(this,"25wehl3u2sa7w");
        RongCloudChatManager.init(this);


    }



    public static MyApplication getApplication() {
        return mInstance;
    }

    /** 获取主线程ID */
    public static int getMainThreadId() {
        return mMainThreadId;
    }

    /** 获取主线程 */
    public static Thread getMainThread() {
        return mMainThread;
    }

    /** 获取主线程的handler */
    public static Handler getMainThreadHandler() {
        return mMainThreadHandler;
    }

    /** 获取主线程的looper */
    public static Looper getMainThreadLooper() {
        return mMainLooper;
    }


}
